import requests
import os
import tos
import sys
import argparse
from tos import HttpMethodType

# 从环境变量获取 AK 和 SK 信息。
ak = sys.argv[1]
sk = sys.argv[2]
endpoint = sys.argv[3]
region = sys.argv[4]
bucket_name = sys.argv[5]
object_key = sys.argv[6]

try:
    
    # 创建 TosClientV2 对象，对桶和对象的操作都通过 TosClientV2 实现
    client = tos.TosClientV2(ak, sk, endpoint, region, is_custom_domain=True)
    # 生成带签名的 url
    print(ak)
    print(sk)
    pre_signed_url_output = client.pre_signed_url(HttpMethodType.Http_Method_Get, bucket_name, object_key)
    print(pre_signed_url_output.signed_url)
    # 使用预签名的url下载对象，以requests为例
    # out = requests.get(pre_signed_url_output.signed_url)
except tos.exceptions.TosClientError as e:
    # 操作失败，捕获客户端异常，一般情况为非法请求参数或网络异常
    print('fail with client error, message:{}, cause: {}'.format(e.message, e.cause))
except tos.exceptions.TosServerError as e:
    # 操作失败，捕获服务端异常，可从返回信息中获取详细错误信息
    print('fail with server error, code: {}'.format(e.code))
    # request id 可定位具体问题，强烈建议日志中保存
    print('error with request id: {}'.format(e.request_id))
    print('error with message: {}'.format(e.message))
    print('error with http code: {}'.format(e.status_code))
    print('error with ec: {}'.format(e.ec))
    print('error with request url: {}'.format(e.request_url))
except Exception as e:
    print('fail with unknown error: {}'.format(e))